<template>
  <a-card :bordered="false" class="card-area">
    <div v-if="!showInfo">
      <div class="table-page-search-wrapper">
        <a-form layout="vertical">
          <a-row :gutter="48">
            <a-col :md="6" :sm="12">
              <a-form-item label="学号">
                <a-input v-model="queryParam.xh"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="姓名">
                <a-input v-model="queryParam.xm"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="性别">
                <a-select allowClear v-model="queryParam.xbm">
                  <a-select-option v-for="i in TYPE0011" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="所在院系">
                <a-select :disablec="selectMenu.yx.length === 1" allowClear showSearch :filterOption="filterOption" v-model="queryParam.ssyxm" @change="yxChange">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="所在专业">
                <a-select allowClear showSearch :filterOption="filterOption" v-model="queryParam.sszym">
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="年级">
                <a-select allowClear v-model="queryParam.sznj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="12">
                <a-form-item label="留学国别">
                  <a-select allowClear v-model="queryParam.lxgbm">
                    <a-select-option v-for="i in TYPE0015" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <!-- <a-col :md="6" :sm="12">
                <a-form-item label="国外导师姓名">
                  <a-input v-model="queryParam.gwdsxm"/>
                </a-form-item>
              </a-col> -->
              <a-col :md="6" :sm="12">
                <a-form-item label="国内导师姓名">
                  <a-input v-model="queryParam.gndsxm"/>
                </a-form-item>
              </a-col>
               <a-col :md="6" :sm="12">
                <a-form-item label="项目状态">
                  <a-select allowClear v-model="queryParam.xmzt">
                    <a-select-option value="0">项目中</a-select-option>
                    <a-select-option value="1">项目结束</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="24" :sm="24">
              <span class="table-page-search-submitButtons" >
                <a-button type="primary" style="margin: 0 8px" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button type="primary" @click="openAdd">批量审核</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
                <a @click="advanced = !advanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
        :scroll="{x:1100}"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <!--<template slot="xh" slot-scope="a,b">-->
          <!--<a @click="direct(b)">{{a}}</a>-->
        <!--</template>-->
          <span slot="gnds" slot-scope="text, record, index">
        {{record.teachers.map(item=>item.xm).join(',')}}
      </span>
        <template slot="operation" slot-scope="a,b">
          <a @click="openEdit(b)">审核</a>
        </template>
      </s-table>

      <audit
        ref="audit"
        :visible.sync="visible"
        :editId.sync="editId"
        @cancel="visible = false"
        @create="handleCreate"
      />
    </div>
    <checkInfo v-else @supplement="showList" :xh.sync="xh"/>
  </a-card>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import {mapState} from 'vuex'
import audit from './gjcaudit'
import checkInfo from './components/checkInfo'

export default {
  name: 'TableList',
  components: {
    STable,
    checkInfo,
    audit
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: true,
      // 查询参数
      queryParam: {},
      postParams: {},
      showInfo: false,
      // 表头
      columns: [
        {
            title: '序号',
            scopedSlots: {customRender: 'serial'},
            width: 60,
            // fixed:'left'
          },
          {
            title: '学号',
            dataIndex: 'xh',
            scopedSlots: {customRender: 'xh'},
            width: 120,
            // fixed:'left'
          },
          {
            title: '姓名',
            dataIndex: 'xm'
          },
          {
            title: '院系',
            dataIndex: 'ssyxmc'
          },
          {
            title: '专业',
            dataIndex: 'sszymc'
          },
          {
            title: '年级',
            dataIndex: 'sznj'
          },
          {
            title: '留学国别',
            dataIndex: 'lxgbmc'
          },
          {
            title: '国外导师',
            dataIndex: 'gwdsxm'
          },
          {
            title: '国内导师',
            dataIndex: 'dedsxm',
            scopedSlots: {customRender: 'gnds'},
          },
          {
            title: '项目状态',
            dataIndex: 'xmzt',
            customRender: (value, row, index) => {
              if(value == '0'){
                  return '项目中'
              }else{
                  return '项目结束'
              }
            }
          },
        {
          title: '操作',
          scopedSlots: { customRender: 'operation' },
          width:80,
          fixed:'right'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        if(!this.initDone){
          return;
        }
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.overseas.gjcUncheckedList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true, // 是否显示alert，已选中
      visible: false,
      confirmLoading: false,
      editId: null,
      selectMenu: {
        yx: [], // 院系
        zy: [] // 所属专业
      },
      initDone: false
    }
  },
  computed: {
    xh () {
      return this.$route.query.xh
    },
    ...mapState({
      TYPE0311: state => state.app['TYPE0311'], // 学业奖学金等级
      TYPE0312: state => state.app['TYPE0312'], // 助研奖学金等级
      TYPE0126: state => state.app['TYPE0126'], // 年级
      TYPE0015: state => state.app['TYPE0015'], // 学生类别
      TYPE0056: state => state.app['TYPE0056'], // 录取类别
      TYPE0083: state => state.app['TYPE0083'], // 入学方式
      TYPE0011: state => state.app['TYPE0011'],
      TYPE0015: state => state.app['TYPE0015']
    })
  },
  // beforeRouteEnter (to, from, next) {
  //   next(vm => {
  //     let pageParams = vm.$store.state.app.pageParams || {}
  //     if (from.path === '/student/stuStatus/search/stuView' && pageParams.path === vm.$route.path) {
  //       vm.queryParam = pageParams.params
  //       vm.parameter = pageParams.pagination
  //     }
  //     vm.initSelectMenu()
  //   })
  // },
  // beforeRouteLeave (to, from, next) {
  //   if (to.path === '/student/stuStatus/search/stuView') {
  //     this.$store.dispatch('app/setPageParams', {
  //       path: this.$route.path,
  //       params: this.queryParam,
  //       pagination: this.parameter
  //     })
  //   }
  //   next()
  // },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0083')
    this.$store.dispatch('app/setDictionary', 'TYPE0015')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0311')
    this.$store.dispatch('app/setDictionary', 'TYPE0312')
    this.$store.dispatch('app/setDictionary', 'TYPE0056')
    this.$store.dispatch('app/setDictionary', 'TYPE0011')
    this.$store.dispatch('app/setDictionary', 'TYPE0015')

    if (this.xh != null && this.xh.length > 0) {
      this.toInfo()
    }
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        // this.$set(this.queryParam, 'sznj', res[1].currentGrade)
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'ssyxm', this.selectMenu.yx[0].dwh)
          this.yxChange(this.queryParam.ssyxm)
        }
        this.initDone = true
        this.$refs.table.refresh(true)
      })
    },
    toInfo () {
      this.showInfo = true
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    showList () {
      this.showInfo = false
      // this.$refs.table.refresh(false)
    },
    handleCreate () {
      const form = this.$refs.audit.form
      form.validateFields((err, values) => {
        if (err) {
          return
        }
        this.$api.overseas.supplementCheck(Object.assign({xh: this.xhs}, values, {explain: values.explain || ' '})).then(res => {
          this.$message.success('操作成功')
          this.$refs.table.refresh(false)
          this.selectedRowKeys = []
          this.selectedRows = []
        })
        form.resetFields()
        this.visible = false
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    openAdd () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要审核的学生')
        return
      }
      this.xhs = this.selectedRows.map(v => {
        return v.xh
      }).toString()
      this.visible = true
      this.$refs.audit.form.resetFields()
    },
    direct (data) {
      this.$router.push(`/overseas/stuStatus/search/stuView?xh=${data.xh}`)
    },
    openEdit (row) {
      this.toInfo()
      this.$router.push(`/overseas/info/uncheckedList/gjcUncheckedList?xh=${row.xh}`)
      // this.visible = true
      // this.xh = row.xh
      // this.$refs.audit.form.resetFields()
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      this.$api.base.majorAll({lsdwh: code}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
    }
  }
}
</script>
